package jblip.gui.components.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Scrollable;

import jblip.gui.data.channels.DataChannel;

public abstract class ChannelViewPanel<T> extends JPanel implements Scrollable,
    Observer {
  private static final long serialVersionUID = 1L;

  private final DataChannel<T> data;

  protected Image bgimage;

  public ChannelViewPanel(final DataChannel<T> channel) {
    this.data = channel;
    this.data.addObserver(this);
    this.bgimage = getDefaultBackground();
    this.setPreferredSize(null);
    this.setOpaque(false);
    this.setVisible(true);
  }

  private Image getDefaultBackground() {
    // BG by Leo L. http://profesorul09.webs.com/index.htm
    return new ImageIcon(ChannelViewPanel.class
        .getResource("/jblip/gui/resources/defbg.png")).getImage();
  }

  @Override
  protected void addImpl(Component comp, Object constraints, int index) {
    super.addImpl(comp, constraints, index);
    setSize(getPreferredSize());
  }

  @Override
  public Color getBackground() {
    return Color.WHITE;
  }

  @Override
  public Dimension getPreferredScrollableViewportSize() {
    return getPreferredSize();
  }

  @Override
  public int getScrollableBlockIncrement(Rectangle visibleRect,
      int orientation, int direction) {
    return 60;
  }

  @Override
  public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
      int direction) {
    return 15;
  }

  @Override
  public boolean getScrollableTracksViewportHeight() {
    return false;
  }

  @Override
  public boolean getScrollableTracksViewportWidth() {
    return true;
  }
}